package com.greenway.tools;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONException;
import org.primefaces.json.JSONObject;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

public class JsonStepParser {

	public static Logger logger = Logger.getLogger(JsonStepParser.class);
	
	public static List<Step> getSteps(JSONObject routs) throws JsonSyntaxException, JSONException{
		
		Gson gson = new Gson();
		ArrayList<Step> stepList = new ArrayList<Step>();
		Step step;
		
		JSONArray legs = routs.getJSONArray("legs");
		JSONArray steps;
		
		for (int i = 0; i < legs.length(); i++) {
			steps = legs.getJSONObject(i).getJSONArray("steps");
			
			for (int j = 0; j < steps.length(); j++) {
				step = gson.fromJson(steps.getJSONObject(j).toString(), Step.class);
				stepList.add(step);
			} 	
		}			
		return stepList;
	}
}
